package com.xteam.youin_supplier.fragment;


import android.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.adpter.TruckTeamListViewAdapter;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.TruckTeamList;
import com.xteam.youin_supplier.util.http.AjaxCallBack;
import com.xteam.youin_supplier.util.view.caculate.CalendarCardPager;
import com.xteam.youin_supplier.util.view.caculate.CardGridItem;
import com.xteam.youin_supplier.util.view.caculate.OnCellItemClick;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 车队列表
 */
@EFragment(R.layout.fragment_truck_team_list)
public class TruckTeamListFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.listview)
    RecyclerView listView;

    @ViewById(R.id.input_time)
    EditText inputTime;

    @ViewById(R.id.company_name)
    EditText companyName;

    @FragmentArg("time")
    String time;
    @FragmentArg("gasstation_id")
    int gasstation_id;

    TruckTeamListViewAdapter truckTeamListViewAdapter;

    TruckTeamListFragment truckTeamListFragment;
    SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");

    @AfterViews
    @Override
    void initView() {
        truckTeamListFragment = this;
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.truck_team_list_title));

        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        loadData();

    }

    private void loadData() {
        requestParams = "gasstation_id=" + gasstation_id
                + "&time=" + (Integer.parseInt(time) + 1);
        requestUrl = RequestAddr.carTeamList + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }


    TruckTeamList truckTeamList;
    private void dealData(Object object) {
        try {
            truckTeamList = gson.fromJson(object.toString(), TruckTeamList.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }

        truckTeamListViewAdapter = new TruckTeamListViewAdapter(truckTeamListFragment, truckTeamList);
        truckTeamListViewAdapter.setOnItemClickListener(new TruckTeamListViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MainActivity.getInstance().switchFragment(truckTeamListFragment, TruckListFragment_.builder()
                .time(time)
                .gasstation_id(gasstation_id)
                .team_id(truckTeamList.getData().get(position).getTeam_id()
                ).build());
            }
        });
        listView.setAdapter(truckTeamListViewAdapter);
    }

    @Click({R.id.back_btn, R.id.input_time, R.id.search_icon})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.search_icon:
            case R.id.input_time:
                showCaculate();
                break;
        }
    }


    AlertDialog alertDialog;

    private void showCaculate() {
        alertDialog = new AlertDialog.Builder(MainActivity.getInstance()).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.caculate_dialog);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        CalendarCardPager calendarCardPager = (CalendarCardPager) window.findViewById(R.id.calendarCard1);
        calendarCardPager.setOnCellItemClick(new OnCellItemClick() {
            @Override
            public void onCellClick(View v, CardGridItem item) {
                String dateStr = item.getDate().get(item.getDate().YEAR)
                        + "-" + (item.getDate().get(item.getDate().MONTH) + 1)
                        + "-" + item.getDate().get(item.getDate().DAY_OF_MONTH);
                Date dateTrans = new Date();
                try {
                    dateTrans = myFmt.parse(dateStr);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                inputTime.setText(myFmt.format(dateTrans) + "");
                alertDialog.dismiss();
            }
        });
    }

}
